// ============================================================================
//
// Copyright (C) 2006-2008 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.test.pipe;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-7-18 qiang.zhang $
 * 
 */
public class PipeApp {

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "main".
     * 
     * @param args
     */
    public static void main(String[] args) {
        PipeApp pipeapp = new PipeApp();
        try {
            FileInputStream xfile = new FileInputStream("c:/input.txt");
            InputStream ylnpipe = pipeapp.changetoy(xfile);
            InputStream zlnpipe = pipeapp.changetoz(ylnpipe);
            System.out.println();
            System.out.println("here are the results");
            BufferedReader inputstream = new BufferedReader(new InputStreamReader(zlnpipe));
            String str = inputstream.readLine();
            while (str != null) {
                System.out.println(str);
                str = inputstream.readLine();
            }
            inputstream.close();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "changetoy".
     * 
     * @param inputstream
     * @return
     */
    public InputStream changetoy(InputStream inputstream) {
        try {
            BufferedReader dinput = new BufferedReader(new InputStreamReader(inputstream));
            PipedOutputStream pipeout = new PipedOutputStream();
            PipedInputStream pipeln = new PipedInputStream(pipeout);
            PrintStream printstream = new PrintStream(pipeout);
            ythread ythread = new ythread(dinput, printstream);
            ythread.start();
            return pipeln;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return null;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "changetoz".
     * 
     * @param inputsteam
     * @return
     */
    public InputStream changetoz(InputStream inputsteam) {
        try {
            BufferedReader dinput = new BufferedReader(new InputStreamReader(inputsteam));
            PipedOutputStream pipeout = new PipedOutputStream();
            PipedInputStream pipeln2 = new PipedInputStream(pipeout);
            PrintStream printstream2 = new PrintStream(pipeout);
            zthread zthread = new zthread(dinput, printstream2);
            zthread.start();
            return pipeln2;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return null;
    }

    class ythread extends Thread {

        BufferedReader xfileln;

        PrintStream printstream;

        ythread(BufferedReader xfileln, PrintStream printstream) {
            this.xfileln = xfileln;
            this.printstream = printstream;
        }

        public void run() {
            try {
                String xstring = xfileln.readLine();
                while (xstring != null) {
                    System.out.println(xstring);
                    String ystring = xstring.replace('x', 'y');
                    printstream.println(ystring);
                    printstream.flush();
                    xstring = xfileln.readLine();
                }
                printstream.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }
    }

    class zthread extends Thread {

        BufferedReader xfileln;

        PrintStream printstream;

        zthread(BufferedReader xfileln, PrintStream printstream) {
            this.xfileln = xfileln;
            this.printstream = printstream;
        }

        public void run() {
            try {
                String xstring = xfileln.readLine();
                while (xstring != null) {
                    System.out.println(xstring);
                    String ystring = xstring.replace('x', 'z');
                    printstream.println(ystring);
                    printstream.flush();
                    xstring = xfileln.readLine();
                }
                printstream.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }
    }
}
